package com.lul15834.orderfood.dao;
import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


import com.lul15834.orderfood.bean.FoodBean;
import com.lul15834.orderfood.db.DBUntil;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 食品数据访问对象
 * 提供对d_FoodBean表的增删查改操作
 */
public class FoodDao {
    public static SQLiteDatabase db= DBUntil.con;

    /**
     * 添加食品
     * @param FoodBean 食品对象
     * @return 插入的行ID
     */
    public long addFoodBean(FoodBean FoodBean) {
        ContentValues values = new ContentValues();
        values.put("s_FoodBean_id", FoodBean.getFoodId());
        values.put("s_business_id", FoodBean.getBusinessId());
        values.put("s_FoodBean_name", FoodBean.getFoodName());
        values.put("s_FoodBean_des", FoodBean.getFoodDescription());
        values.put("s_FoodBean_price", FoodBean.getFoodPrice());
        values.put("s_FoodBean_img", FoodBean.getFoodImage());

        return db.insert("d_FoodBean", null, values);
    }

    /**
     * 根据ID查询食品
     * @param FoodBeanId 食品ID
     * @return 食品对象，未找到返回null
     */
    public FoodBean getFoodBeanById(String FoodBeanId) {
        Cursor cursor = db.query("d_FoodBean",
                new String[]{"s_FoodBean_id", "s_business_id", "s_FoodBean_name", "s_FoodBean_des", "s_FoodBean_price", "s_FoodBean_img"},
                "s_FoodBean_id = ?",
                new String[]{FoodBeanId},
                null, null, null);

        if (cursor != null && cursor.moveToFirst()) {
            FoodBean FoodBean = cursorToFoodBean(cursor);
            cursor.close();
            return FoodBean;
        }
        return null;
    }

    /**
     * 获取当前月的销售数量
     * @param foodId
     * @return
     */
    @SuppressLint("Range")
    public static int getMouSaleNum(String foodId){
        int saleNum=0;
        Cursor rs = db.rawQuery("select * from d_orders " +
                "where s_order_sta='3' and strftime('%Y-%m',s_order_time)='2025-04';",null);
//        strftime('%Y-%m','now')
        List<String> list=new ArrayList<>();
        while (rs.moveToNext()){
            String temp=rs.getString(rs.getColumnIndex("s_order_id"));
            list.add(temp);
        }
        for (String s : list) {
            saleNum=saleNum+getOrderDetailsByOrderAndFoodId(s,foodId);
        }
        return saleNum;
    }

    /**
     * 通过订单ID获取商品详情，商品ID
     * @param orderId
     * @param foodId
     * @return
     */
    @SuppressLint("Range")
    public static int getOrderDetailsByOrderAndFoodId(String orderId,String foodId){
        String data[]={orderId,foodId};
        Cursor rs = db.rawQuery("select * from d_order_details " +
                "where s_details_id=? and s_food_id=?",data);
        List<String> list=new ArrayList<>();
        while (rs.moveToNext()){
            int tm=rs.getInt(rs.getColumnIndex("s_food_num"));
            return tm;
        }
        return 0;
    }

    /**
     * 实现添加商品
     * @param businessId
     * @param foodName
     * @param des
     * @param foodPrice
     * @param img
     * @return
     */
    public static int addFood(String businessId,String foodName,String des,String foodPrice,String img){
        //把横杠替换为空
        String id= UUID.randomUUID().toString().replace("-","");
        String data[]={id,businessId,foodName,des,foodPrice,img};
        try {
            db.execSQL("INSERT INTO d_food (s_food_id, s_business_id, s_food_name, s_food_des, s_food_price, s_food_img) " +
                            "VALUES (?, ?, ?, ?, ?, ?)",
                    data);
            return 1;
        }catch (Exception e){
            return 0;
        }

    }

    /**
     * 实现更改食物
     * @param foodId
     * @param foodName
     * @param des
     * @param foodPrice
     * @param img
     * @return
     */
    public static int updateFood(String foodId,String foodName,String des,String foodPrice,String img){
        String data[]={foodName,des,foodPrice,img,foodId};
        try {
            db.execSQL("update d_food set s_food_name=?,s_food_des=?,s_food_price=?,s_food_img=?" +
                            " where s_food_id=? ", data);
            return 1;
        }catch (Exception e){
            return 0;
        }

    }

    /**
     * 实现删除食物
     * @param foodId
     * @return
     */
    public static int delFoodById(String foodId){
        String data[]={foodId};
        try {
            db.execSQL("delete from d_food where s_food_id=? ", data);
            return 1;
        }catch (Exception e){
            return 0;
        }

    }
    /**
     * 查询商家所有食品
     * @param id 商家ID
     * @return 食品列表
     */
    public static List<FoodBean> getAllFoodById(String id){
        List<FoodBean> list=new ArrayList<>();
        Cursor cursor=db.rawQuery("select * from d_food where s_business_id=?",new String[]{id});
        while (cursor.moveToNext()){
            @SuppressLint("Range") String food_id=cursor.getString(cursor.getColumnIndex("s_food_id"));
            @SuppressLint("Range") String business_id=cursor.getString(cursor.getColumnIndex("s_business_id"));
            @SuppressLint("Range") String food_name=cursor.getString(cursor.getColumnIndex("s_food_name"));
            @SuppressLint("Range") String food_price=cursor.getString(cursor.getColumnIndex("s_food_price"));
            @SuppressLint("Range") String food_des=cursor.getString(cursor.getColumnIndex("s_food_des"));
            @SuppressLint("Range") String food_img=cursor.getString(cursor.getColumnIndex("s_food_img"));
            FoodBean foodBean=new FoodBean();
            foodBean.setFoodId(food_id);
            foodBean.setBusinessId(business_id);
            foodBean.setFoodDescription(food_des);
            foodBean.setFoodName(food_name);
            foodBean.setFoodPrice(food_price);
            foodBean.setFoodImage(food_img);
            list.add(foodBean);
        }
        return list;
    }
    public static FoodBean getAllFoodByFoodId(String id){
        FoodBean foodBean=new FoodBean();
        Cursor cursor=db.rawQuery("select * from d_food where s_food_id=?",new String[]{id});
        while (cursor.moveToNext()){
            @SuppressLint("Range") String food_id=cursor.getString(cursor.getColumnIndex("s_food_id"));
            @SuppressLint("Range") String business_id=cursor.getString(cursor.getColumnIndex("s_business_id"));
            @SuppressLint("Range") String food_name=cursor.getString(cursor.getColumnIndex("s_food_name"));
            @SuppressLint("Range") String food_price=cursor.getString(cursor.getColumnIndex("s_food_price"));
            @SuppressLint("Range") String food_des=cursor.getString(cursor.getColumnIndex("s_food_des"));
            @SuppressLint("Range") String food_img=cursor.getString(cursor.getColumnIndex("s_food_img"));
            foodBean.setFoodId(food_id);
            foodBean.setBusinessId(business_id);
            foodBean.setFoodDescription(food_des);
            foodBean.setFoodName(food_name);
            foodBean.setFoodPrice(food_price);
            foodBean.setFoodImage(food_img);
        }
        return foodBean;
    }


    /**
     * 查询所有食品
     * @return 所有食品列表
     */
    public static List<FoodBean> getAllFoodBeans(){
        List<FoodBean> list=new ArrayList<>();
        Cursor cursor=db.rawQuery("select * from d_food ",null);
        while (cursor.moveToNext()){
            @SuppressLint("Range") String food_id=cursor.getString(cursor.getColumnIndex("s_food_id"));
            @SuppressLint("Range") String business_id=cursor.getString(cursor.getColumnIndex("s_business_id"));
            @SuppressLint("Range") String food_name=cursor.getString(cursor.getColumnIndex("s_food_name"));
            @SuppressLint("Range") String food_price=cursor.getString(cursor.getColumnIndex("s_food_price"));
            @SuppressLint("Range") String food_des=cursor.getString(cursor.getColumnIndex("s_food_des"));
            @SuppressLint("Range") String food_img=cursor.getString(cursor.getColumnIndex("s_food_img"));
            FoodBean foodBean=new FoodBean();
            foodBean.setFoodId(food_id);
            foodBean.setBusinessId(business_id);
            foodBean.setFoodDescription(food_des);
            foodBean.setFoodName(food_name);
            foodBean.setFoodPrice(food_price);
            foodBean.setFoodImage(food_img);
            list.add(foodBean);
        }
        return list;
    }
    public static List<FoodBean> getAllFoodList(String businessId,String title){
        String search="%"+title.trim()+"%";
        String data[]={search,businessId};
        List<FoodBean> list=new ArrayList<>();
        Cursor cursor=db.rawQuery("select * from d_food where s_food_name like ? and s_business_id=?",data);
        while (cursor.moveToNext()){
            @SuppressLint("Range") String food_id=cursor.getString(cursor.getColumnIndex("s_food_id"));
            @SuppressLint("Range") String business_id=cursor.getString(cursor.getColumnIndex("s_business_id"));
            @SuppressLint("Range") String food_name=cursor.getString(cursor.getColumnIndex("s_food_name"));
            @SuppressLint("Range") String food_price=cursor.getString(cursor.getColumnIndex("s_food_price"));
            @SuppressLint("Range") String food_des=cursor.getString(cursor.getColumnIndex("s_food_des"));
            @SuppressLint("Range") String food_img=cursor.getString(cursor.getColumnIndex("s_food_img"));
            FoodBean foodBean=new FoodBean();
            foodBean.setFoodId(food_id);
            foodBean.setBusinessId(business_id);
            foodBean.setFoodDescription(food_des);
            foodBean.setFoodName(food_name);
            foodBean.setFoodPrice(food_price);
            foodBean.setFoodImage(food_img);
            list.add(foodBean);
        }
        return list;
    }
    public static List<FoodBean> getAllFoodListUser(String title){
        String search="%"+title.trim()+"%";
        String data[]={search};
        List<FoodBean> list=new ArrayList<>();
        Cursor cursor=db.rawQuery("select * from d_food where s_food_name like ?  ",data);
        while (cursor.moveToNext()){
            @SuppressLint("Range") String food_id=cursor.getString(cursor.getColumnIndex("s_food_id"));
            @SuppressLint("Range") String business_id=cursor.getString(cursor.getColumnIndex("s_business_id"));
            @SuppressLint("Range") String food_name=cursor.getString(cursor.getColumnIndex("s_food_name"));
            @SuppressLint("Range") String food_price=cursor.getString(cursor.getColumnIndex("s_food_price"));
            @SuppressLint("Range") String food_des=cursor.getString(cursor.getColumnIndex("s_food_des"));
            @SuppressLint("Range") String food_img=cursor.getString(cursor.getColumnIndex("s_food_img"));
            FoodBean foodBean=new FoodBean();
            foodBean.setFoodId(food_id);
            foodBean.setBusinessId(business_id);
            foodBean.setFoodDescription(food_des);
            foodBean.setFoodName(food_name);
            foodBean.setFoodPrice(food_price);
            foodBean.setFoodImage(food_img);
            list.add(foodBean);
        }
        return list;
    }
    /**
     * 更新食品信息
     * @param FoodBean 食品对象
     * @return 受影响的行数
     */
    public int updateFoodBean(FoodBean FoodBean) {
        ContentValues values = new ContentValues();
        values.put("s_business_id", FoodBean.getBusinessId());
        values.put("s_FoodBean_name", FoodBean.getFoodName());
        values.put("s_FoodBean_des", FoodBean.getFoodDescription());
        values.put("s_FoodBean_price", FoodBean.getFoodPrice());
        values.put("s_FoodBean_img", FoodBean.getFoodImage());

        return db.update("d_FoodBean", values, "s_FoodBean_id = ?", new String[]{FoodBean.getFoodId()});
    }

    /**
     * 删除食品
     * @param FoodBeanId 食品ID
     * @return 删除的行数
     */
    public int deleteFoodBean(String FoodBeanId) {
        return db.delete("d_FoodBean", "s_FoodBean_id = ?", new String[]{FoodBeanId});
    }

    /**
     * 删除商家所有食品
     * @param businessId 商家ID
     * @return 删除的行数
     */
    public int deleteFoodBeansByBusiness(String businessId) {
        return db.delete("d_FoodBean", "s_business_id = ?", new String[]{businessId});
    }

    /**
     * 将Cursor转换为FoodBean对象
     */
    private FoodBean cursorToFoodBean(Cursor cursor) {
        FoodBean FoodBean = new FoodBean();
        FoodBean.setFoodId(cursor.getString(0));
        FoodBean.setBusinessId(cursor.getString(1));
        FoodBean.setFoodName(cursor.getString(2));
        FoodBean.setFoodDescription(cursor.getString(3));
        FoodBean.setFoodPrice(cursor.getString(4));
        FoodBean.setFoodImage(cursor.getString(5));
        return FoodBean;
    }
}
